# = test.loginlogout.1.rb - Login and Logout Test
# See LICENSE.txt for licensing details.
#
# == Description
#To test the functionality to verify Login and Log out 

require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
    
	class TC_LoginLogout_test < Test::Unit::TestCase
	  def startup			
      @@ie = Watir::IE.start($SITE_URL)
	  end
	
	  def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
	  end
    
    def test_01_verifyHomeText
      assert(ie.contains_text(/Community Server/))		
	  end
  
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
      ie.link(:text, 'Sign in').click
      assert(ie.contains_text("Sign in name"))
    end
    
    def test_04_verifyLinksOnLoginPage
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.button(:id, /TitleBarSearchButton$/).exists?)
        assert(ie.link(:text, 'Home').exists?)
        assert(ie.link(:text, 'Blogs').exists?)
        assert(ie.link(:text, 'Forums').exists?)
        assert(ie.link(:text, 'Photos').exists?)
        assert(ie.link(:text, 'Downloads').exists?)
        assert(ie.link(:text, 'I forgot my password').exists?)

        assert_equal $SITE_URL, ie.link(:text, 'Home').href
        assert_equal $SITE_URL + 'blogs/', ie.link(:text, 'Blogs').href
        assert_equal $SITE_URL + 'forums/', ie.link(:text, 'Forums').href
        assert_equal $SITE_URL + 'photos/', ie.link(:text, 'Photos').href
        assert_equal $SITE_URL + 'files/', ie.link(:text, 'Downloads').href
    end
	  
	  def test_05_populateLoginForm
      ie.text_field(:id, /username$/).value = $ADMIN_USER
      assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
      ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
      assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_06_submitLoginForm
      ie.link(:id, /loginButton$/).click
      test_01_verifyHomeText
    end

    def test_07_verifyLogoutLinkExists
      assert_equal $SITE_URL + 'logout.aspx', ie.link(:text, 'Sign out').href
    end
    
    def test_08_navigateToLogout
      ie.link(:text, 'Sign out').click
      assert(ie.contains_text("Return home"))
    end
    
    def test_09_verifyLogoutPageLinks
      assert(ie.link(:text, 'Help').exists?)
      assert(ie.link(:text, 'Home').exists?)
      assert(ie.link(:text, 'Blogs').exists?)
      assert(ie.link(:text, 'Forums').exists?)
      assert(ie.link(:text, 'Photos').exists?)
      assert(ie.link(:text, 'Downloads').exists?)
      assert(ie.link(:text, 'Sign in').exists?)
      assert(ie.link(:text, 'Return home').exists?)
      
      assert_equal $SITE_URL, ie.link(:text, 'Home').href
      assert_equal $SITE_URL + 'blogs/', ie.link(:text, 'Blogs').href
      assert_equal $SITE_URL + 'forums/', ie.link(:text, 'Forums').href
      assert_equal $SITE_URL + 'photos/', ie.link(:text, 'Photos').href
      assert_equal $SITE_URL + 'files/', ie.link(:text, 'Downloads').href
      assert_equal $SITE_URL, ie.link(:text, 'Return home').href
    end
    
    def test_99_closeIE
      ie.close
    end
  
  end